function [ EC ] = APEanimateAVI( anidatax, anidatay, outname, frmrate, ...
    qulity, includeaxis)

%[ EC ] = APEanimateAVI( anidatax, anidatay, framerate, outname )
%
% Script to animate kinetic datasets to an AVI video file
%
% INPUT     anidatax    :   X data for animation
%           anidatay    :   Y data for animation
%           outname     :   filename for AVI
%           frmrate     :   frames per second of AVI ([30])
%           qulity      :   Quality in percent ([75])     
%           includeaxis :    0 = only plot area
%                           [1]= include Axis etc.
%
% OUTPUT    EC          :   error code (0 = success, 1 = error)
%

%  (c) F. Dworkowski, 2011
%      Version 1.0, 2011-09-26

%% ===== VALIDATION =====
if nargin < 3
    help APEanimateAVI
    EC = 1;
    return
end

%% ===== CONFIG =====

vidObj = VideoWriter([outname,'.avi']);
if exist('frmrate','var') == 1
    frmrate = floor(frmrate);
    vidObj.FrameRate = frmrate;
end
if exist('qulity','var') == 1
    qulity = floor(qulity);
    vidObj.Quality = qulity;
end
if exist('includeaxis','var') == 0
    includeaxis = 1;        % set 0 for only graph area, 1 for including axis
elseif includeaxis ~= [0 1]
    errordlg({'ERROR:';' "includeaxis" was not'; '  defined properly!'})
    EC = 1;
    return
end

%% ===== CODE =====
open(vidObj);

plot(anidatax, anidatay(:,1)) 
axis([min(anidatax) max(anidatax) min(min(anidatay(:,1:(end-1)))) max(max(anidatay))])
set(gca,'nextplot','replacechildren');

h = warndlg({'Adjust plot to desired'; 'size and press OK'});
uiwait(h)

for k = 1:length(anidatay(1,:)) 
   plot(anidatax, anidatay(:,k))
   
   % Write each frame to the file.
   if includeaxis == 1
       currFrame = getframe(gcf);
   else
       currFrame = getframe;
   end
   writeVideo(vidObj,currFrame);
end

%% ===== CLOSING =====
close(vidObj);
EC = 0;
end

